﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using DevExpress.XtraTreeList.Nodes;

namespace SmartFrame_DataCollect
{
    public partial class LogView : BaseModule
    {
        string logDirectoryPath = AppDomain.CurrentDomain.BaseDirectory + @"DataCollectService\Log\LogDirectory";
        public LogView()
        {
            InitializeComponent();
            InitTree();
        }

        //初始化树节点
        private void InitTree()
        {
            
            var dateDirectories = Directory.GetDirectories(logDirectoryPath).ToList();
            DataTable dtDirectory = new DataTable();
            dtDirectory.Columns.Add("name");
            dtDirectory.Columns.Add("parent");
            dtDirectory.Columns.Add("value");
            dtDirectory.Columns.Add("id");
            int i = 0;
            foreach (var path in dateDirectories)
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(path);
                DataRow dr = dtDirectory.NewRow();
                dr["name"] = directoryInfo.Name;
                dr["parent"] = "";
                dr["id"] = i.ToString();
                dr["value"] = directoryInfo.FullName;
                dtDirectory.Rows.Add(dr);

                var files = directoryInfo.GetFiles();
                int j = 0;
                foreach (FileInfo item in files)
                {
                    DataRow dr_c = dtDirectory.NewRow();
                    dr_c["name"] = item.Name;
                    dr_c["parent"] = i.ToString();
                    dr_c["id"] = i+"_"+j;
                    dr_c["value"] = item.FullName;
                    j++;
                    dtDirectory.Rows.Add(dr_c);
                }
                i++;

            }
            
            treeList1.KeyFieldName = "id";
            treeList1.ParentFieldName = "parent";
            treeList1.DataSource = dtDirectory;

        }

        private void treeList1_Click(object sender, EventArgs e)
        {
            DataRow selectRow = treeList1.GetFocusedDataRow();
            if (selectRow["parent"].ToString()!="")
            {
                richEditControl1.LoadDocument(selectRow["value"].ToString());
            }
        }
    }

}
